package DAO;


import javax.persistence.*;


public class DAOJPA {

	private static EntityManagerFactory emf = null;
	private static EntityManager em = null;
	private static EntityTransaction tx = null;

	public static EntityManager getManager() {
		if (em == null) {
			emf = Persistence.createEntityManagerFactory("projet_adw");
			em = emf.createEntityManager();
			tx = em.getTransaction();
		}
		if (!tx.isActive()) {
			tx.begin();
		}
		return em;

	}

	public static void commit() {
		em.flush();
		tx.commit();
	}
	
	public static void rollback() {
		tx.rollback();
	}
	
	public static void close() {
		em.close(); em = null;
		emf.close();emf = null;
	}

	public static void viderBase() {
		
		
		getManager().createNativeQuery("DELETE FROM produit").executeUpdate();
		getManager().createNativeQuery("DELETE FROM joue").executeUpdate();
		getManager().createNativeQuery("DELETE FROM realise").executeUpdate();
		
		getManager().createNativeQuery("DELETE FROM personne").executeUpdate();
		getManager().createNativeQuery("ALTER TABLE personne AUTO_INCREMENT = 1").executeUpdate();
		
		getManager().createNativeQuery("DELETE FROM film").executeUpdate();
		getManager().createNativeQuery("ALTER TABLE film AUTO_INCREMENT = 1").executeUpdate();
		
		getManager().createQuery("DELETE FROM Recompense").executeUpdate();
		getManager().createNativeQuery("ALTER TABLE recompense AUTO_INCREMENT = 1").executeUpdate();
		
		}



}